<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div><input type="text" id="mobile" size="11" value="13950076987" /><button type="button" id="btnsend" onclick="sendsms();">发送验证码</button></div>
        <script type="text/javascript">
            function sendsms() {
                var mobile = document.getElementById('mobile').value;
                if (mobile.length < 10) {
                    alert('验证码无效');
                    return;
                }
                ajaxget('smssrv.php?mobile=' + mobile, function (msg) {
					console.log(typeof(msg));
					if(typeof(msg)=='string'){
					msg=JSON.parse(msg);
					}
					console.log(msg);
                    if (msg.errcode==0) {
                        alert('验证码已发送');
                    } else {
                        alert(msg.errmsg);
                    }
                });

            }
            function ajaxget(url, handler) {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                        var response = xhr.responseText;
                        if (handler) {
                            handler(response);
                        }
                        console.log(response);
                    }
                };
                xhr.open("GET", url, true);
                xhr.send();
            }
        </script>
    </body>
</html>
